Skip to content

test(agent-server): cover budget denial after managed-key refresh - #5378

Merged
ak684 merged 2 commits into
mainfrom
alona/managed-key-budget-regression
Sep 29, 2026
Merged

ak684 merged 2 commits into
mainfrom
alona/managed-key-budget-regression

Conversation

@ak684

@ak684 ak684 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

Adds regression tests proving that a stale managed key can refresh, an exhausted budget then fails without retries, and
the same LLM succeeds after the limit is raised


AGENT:

Why

Managed-key recovery and budget fail-fast are tested separately. This adds coverage for a stale key that refreshes successfully but then hits an exhausted budget.

Summary

  • Exercise sync and async calls over real local HTTP through the Agent Server refresh hook and SDK transport.
  • Assert one stale-key request, one refreshed-key budget denial, and one authenticated refresh, with no budget retries.
  • Lift the fixture budget and verify the same LLM instance succeeds on the next call.

Issue Number

Related to #5189: regression coverage for managed-key recovery followed by budget denial. Does not close the hosted-account issue or claim deployed recovery validation. Builds on merged PR 5222 and PR 5309.

How to Test

Validated on current main d62d87e79, which includes both dependencies:

uv run pytest tests/agent_server/test_managed_llm_key_http.py tests/agent_server/test_managed_llm_key.py tests/sdk/llm/test_budget_denial_http.py tests/sdk/llm/test_auth_refresh_retry.py -q

103 passed. Changed-file pre-commit passes. Fresh CI runs on the rebased branch.

Before #5309, both new cases failed with 10 provider requests instead of 2. With the merged fix, stale-key refresh and budget denial make exactly two requests, and the same LLM succeeds after lifting the fixture budget.

Video/Screenshots

No UI change. The fixture starts a real local HTTP server with synthetic credentials and controlled 401/429/200 responses. This validates SDK transport and registered refresh behavior; it does not prove deployed Enterprise/LiteLLM behavior, conversation persistence, or UI Retry.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

No production code changes. The same LLM instance is reused; full same-conversation recovery remains part of packaged acceptance.


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:c5d1717-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-c5d1717-python \
  ghcr.io/openhands/agent-server:c5d1717-python

All tags pushed for this build

ghcr.io/openhands/agent-server:c5d1717-golang-amd64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-golang-amd64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-golang-amd64
ghcr.io/openhands/agent-server:c5d1717-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:c5d1717-golang-arm64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-golang-arm64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-golang-arm64
ghcr.io/openhands/agent-server:c5d1717-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:c5d1717-java-amd64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-java-amd64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-java-amd64
ghcr.io/openhands/agent-server:c5d1717-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:c5d1717-java-arm64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-java-arm64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-java-arm64
ghcr.io/openhands/agent-server:c5d1717-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:c5d1717-python-amd64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python-amd64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python-amd64
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime-amd64
ghcr.io/openhands/agent-server:c5d1717-python-arm64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python-arm64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python-arm64
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime-arm64
ghcr.io/openhands/agent-server:c5d1717-python-minimal-amd64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python-minimal-amd64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python-minimal-amd64
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:c5d1717-python-minimal-arm64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python-minimal-arm64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python-minimal-arm64
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:c5d1717-python-slim-amd64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python-slim-amd64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python-slim-amd64
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:c5d1717-python-slim-arm64
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python-slim-arm64
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python-slim-arm64
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:c5d1717-golang
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-golang
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-golang
ghcr.io/openhands/agent-server:c5d1717-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:c5d1717-java
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-java
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-java
ghcr.io/openhands/agent-server:c5d1717-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:c5d1717-python-minimal
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python-minimal
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python-minimal
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime-minimal
ghcr.io/openhands/agent-server:c5d1717-python-slim
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python-slim
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python-slim
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime-slim
ghcr.io/openhands/agent-server:c5d1717-python
ghcr.io/openhands/agent-server:c5d171756cf2cbbef014a2224c0fe8f9d6e132dc-python
ghcr.io/openhands/agent-server:alona-managed-key-budget-regression-python
ghcr.io/openhands/agent-server:c5d1717-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., c5d1717-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., c5d1717-python-amd64) are also available if needed

Base automatically changed from feat/agent-server-register-refresh-hook-5189 to main September 29, 2026 18:06
Co-authored-by: openhands <openhands@all-hands.dev>
@ak684
ak684 force-pushed the alona/managed-key-budget-regression branch from c97fe6f to 7d7a94d Compare September 29, 2026 19:31
@ak684
ak684 marked this pull request as ready for review September 29, 2026 19:31
@ak684
ak684 requested a review from all-hands-bot September 29, 2026 19:31
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
TOTAL456761874659% 
report-only-changed-files is enabled. No changed files were found in the coverage report :)

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Assessment

Test-only change to tests/agent_server/test_managed_llm_key_http.py (+41/-2). No production code is touched. The new regression is well constructed and the behavior it claims is genuinely exercised, but it cannot be approved as-is because a repository gate is red on this exact head.

Verified on head 7d7a94d4db5dbde7d3b962b7117615dc569c009f:

  • uv run pytest tests/agent_server/test_managed_llm_key_http.py tests/agent_server/test_managed_llm_key.py tests/sdk/llm/test_budget_denial_http.py tests/sdk/llm/test_auth_refresh_retry.py -q -> 103 passed.
  • The end-to-end test paths are real: a ThreadingHTTPServer serves 401/429/200, and the assertions run through the SDK transport and the agent-server refresh hook (register_managed_llm_key_refresh). No mocks substitute for the code under test.

What the change gets right

  • The new case inverts the pre-existing fixture in a meaningful way: the stale key is rejected (401) but the refreshed key is accepted, then the provider denies on budget. That is the only combination that distinguishes "refresh succeeded but budget is exhausted" from "refresh failed", so the added coverage is not redundant with test_managed_refresh_is_bounded_over_http.
  • LLMRateLimitError is the correct mapped exception. is_budget_exceeded_error() (openhands-sdk/openhands/sdk/llm/exceptions/classifier.py) matches on the budget_exceeded payload type, is_quota_exhaustion_error() then excludes it from the retry predicate in LLM._make_retry_decorator(), and max_retries=0 is passed at the LiteLLM boundary. Asserting exactly ["Bearer stale-key", "Bearer fresh-key"] therefore genuinely proves no budget retry occurred; a retrying implementation would emit more than two POSTs.
  • Reusing the same LLM instance for the follow-up call and asserting the request log doubles to four entries verifies that recovery after the limit is lifted works on the existing instance, not just on a fresh one.
  • Both sync and async are parametrized.
  • Adding a leading system message to messages() now satisfies the repository's "system message before the first user message" invariant in DEVELOPMENT.md/AGENTS.md.
  • Import ordering and the new LLMRateLimitError import are fine; pre-commit passes.

Actionable finding

The Validate PR description check is failing on head 7d7a94d4 (run 36619818894, job 109582043809). The job log reports:

Linked issue(s) (#5222, #5309) carry neither ready-for-dev nor a pre-rollout creation date. Newly referenced issues must meet the readiness criteria before a PR can be opened.

## Issue Number currently reads "Builds on merged #5222 and #5309...". .github/scripts/check_pr_description.py scans that section for bare #NNN tokens (BARE_ISSUE_REF_RE) and treats them as linked issues; both referenced items were created after the 2026-08-13 cutoff and carry no ready-for-dev label, so the gate fails deterministically. These are merged PRs, not issues awaiting development, so the gate is misreading an informational reference. This is not a code defect, but it is a concrete, merge-blocking CI failure on the current head and should be fixed before the PR lands - for example by referring to them as "PR 5222" / "PR 5309" in the ## Issue Number section (or linking an actual ready-for-dev issue) and skipping the bare #NNN form.

Notes

The fixture budget assertion matches how a real managed proxy sends LiteLLM budget denials, and the PR description correctly limits the claim (it does not claim to prove deployed Enterprise/LiteLLM or UI behavior), which is honest scoping. Nothing in the changed file is out of scope for this repository.

🔄 CHANGES REQUESTED

@ak684
ak684 requested a review from all-hands-bot September 29, 2026 19:42
@ak684

ak684 commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

Updated the Issue Number section to link the existing managed-key issue #5189, which is ready-for-dev. The description explicitly limits this PR to regression coverage and does not claim to close the hosted issue. No code changed.

@neubig
neubig requested review from all-hands-bot and removed request for all-hands-bot September 29, 2026 21:14

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Assessment

Test-only change to tests/agent_server/test_managed_llm_key_http.py (+41/-2). No production code is touched. The change is in scope for this repository (Agent Server test suite) and the previously blocking gate is now resolved on this head.

Verified on head c5d171756cf2cbbef014a2224c0fe8f9d6e132dc:

  • The PR diff is exactly one file (tests/agent_server/test_managed_llm_key_http.py); the merge of main brought in only version/lockfile churn outside the PR's file set.
  • uv run pytest tests/agent_server/test_managed_llm_key_http.py tests/agent_server/test_managed_llm_key.py tests/sdk/llm/test_budget_denial_http.py tests/sdk/llm/test_auth_refresh_retry.py -q -> 103 passed.
  • All current-head checks are green: 28 success, 2 skipped, 0 failures, including Validate PR description, pre-commit, agent-server-tests-family checks, sdk-tests, and the binary/build jobs.
  • The linked issue #5189 carries ready-for-dev, and the PR description now limits its claim ("Related to #5189 ... Does not close"), so the description gate passes.

What the change gets right

  • The new case is not redundant with test_managed_refresh_is_bounded_over_http: it exercises the only combination that distinguishes "refresh succeeded but the budget is exhausted" from "refresh failed" - stale key rejected (401), refreshed key accepted, then a budget denial.
  • LLMRateLimitError is the correct mapped exception. is_budget_exceeded_error() (openhands-sdk/openhands/sdk/llm/exceptions/classifier.py) matches the budget_exceeded payload, is_quota_exhaustion_error() excludes it from the retry predicate in LLM._make_retry_decorator(), and max_retries=0 is passed at the LiteLLM boundary. Asserting exactly ["Bearer stale-key", "Bearer fresh-key"] therefore genuinely proves no budget retry occurred; a retrying implementation would emit more than two POSTs.
  • Reusing the same LLM instance for the follow-up call and asserting the request log doubles to four entries verifies recovery on the existing instance, not just on a fresh one.
  • Both sync and async paths are parametrized, and the leading system message now satisfies the repository's system-before-user message invariant.
  • The evidence is real end-to-end HTTP (a ThreadingHTTPServer serving 401/429/200) through the SDK transport and the agent-server refresh hook; no mocks substitute for the code under test.

No material correctness, security, compatibility, or acceptance-criterion defect found on this head. The prior description-gate finding is resolved.

✅ APPROVED

@ak684
ak684 merged commit 2b9502c into main Sep 29, 2026
55 checks passed
@ak684
ak684 deleted the alona/managed-key-budget-regression branch September 29, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants